package com.tubang.component.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RequestUtil {
	private static Logger logger = LoggerFactory.getLogger(RequestUtil.class);

	@SuppressWarnings("deprecation")
	public static String receivePostJson(HttpServletRequest request) {
        try {
			// 读取请求内容
			BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while((line = br.readLine())!=null){
			    sb.append(line);
			}
			//将数据解码
			String reqBody = sb.toString();
			String receivePostJson = URLDecoder.decode(reqBody, HTTP.UTF_8);
			logger.info("接收到的请求为： receivePostJson == >" + receivePostJson);
			return receivePostJson;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
    }
	
}
